<?php

namespace App\Http\Services;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use \Exception;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;


class SmsService
{
    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dysmsapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([
            // 必填，您的 AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 必填，您的 AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }


    /**
     * @param string[] $args
     * @return void
     */
    public static function send($templateCode,$phoneNumbers,$templateParam)
    {
        $templateParam = json_encode($templateParam);
        // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/311677.html
        $client = self::createClient("LTAI5tSTig6ZB88UDoDrvuEx", "VeHv92GoEw1vPo0CHHj4xloIex68HP");
        $sendSmsRequest = new SendSmsRequest([
            'phoneNumbers' => $phoneNumbers,
            'signName' => '花石木',
            'templateCode' => $templateCode,
            'templateParam' => $templateParam
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            // 复制代码运行请自行打印 API 的返回值
           $result = $client->sendSmsWithOptions($sendSmsRequest, $runtime);

           if (isset($result->body->code) && $result->body->code=='OK'){
                return true;
           }
           return false;
        } catch (Exception $error) {
            return false;
        }
    }
}
